package com.company.cloud.page.modules.mgr.controller;

import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.page.modules.mgr.controller.req.PageConfigUpdateStateParam;
import com.company.cloud.page.modules.mgr.service.PageConfigMgrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.constant.BaseConstant.EnabledEnum;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.page.modules.base.entity.PageConfigInfo;
import com.company.cloud.page.modules.base.service.PageConfigInfoService;
import com.company.cloud.page.modules.cache.models.PageInfoModel;
import com.company.cloud.page.modules.cache.service.PageCacheService;
import com.company.cloud.page.modules.cache.utils.PageCachekeys;
import com.company.cloud.page.modules.cache.utils.PageConvertUtils;
import com.company.cloud.page.modules.mgr.controller.req.PageConfigQuery4PageParam;
import com.company.cloud.page.modules.mgr.controller.req.PageConfigSaveParam;
import com.company.cloud.page.modules.mgr.controller.req.PageConfigUpdateParam;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 页面配置信息管理
 */
@Authc
@RestController
@RequestMapping("/mgr/cms/pageConfig")
@Api(tags = "Page_Mgr_页面配置信息管理")
public class PageConfigController {
	
	@Autowired
	private PageConfigInfoService pageConfigInfoService;
	@Autowired
	private PageCacheService pageCacheService;
	@Autowired
	private PageConfigMgrService pageConfigMgrService;
	
	@PostMapping("/query")
	@ApiOperation(value="列表查询", response= PageInfoModel.class)
	public BaseResult query(@RequestBody PageConfigQuery4PageParam params) {
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		params.setCompanyNo(userInfo.getCompanyNo());
		params.setContent(false);
		IPage<PageInfoModel> data=pageConfigMgrService.query(params);
		return BaseResult.ok(data);
	}

	@GetMapping("/get")
	@ApiOperation(value="详情",response = PageInfoModel.class)
	public BaseResult get(@RequestParam("pageNo")String pageNo) {
		PageInfoModel data= pageCacheService.getPageConfigDB(pageNo);
		return BaseResult.ok(data);
	}

	@PostMapping("/save")
	@ApiOperation(value="新增",response = BaseResult.class)
	public BaseResult save(@RequestBody PageConfigSaveParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		params.setCompanyNo(userInfo.getCompanyNo());
		params.setIsTemplate(BaseConstant.YNEnum.NO.getCode());
		if(StringUtils.isEmpty(params.getState())){
			params.setState(BaseConstant.YNEnum.NO.getCode());
		}
		pageConfigMgrService.save(params,userInfo);
		return BaseResult.ok();
	}
	
	@PostMapping("/update")
	@ApiOperation(value="修改",response = BaseResult.class)
	public BaseResult update(@RequestBody PageConfigUpdateParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		PageConfigInfo entity= pageConfigMgrService.update(params,userInfo);
		pageCacheService.removePageConfigCache(entity.getPageNo());
		return BaseResult.ok();
	}

	@PostMapping("/updateState")
	@ApiOperation(value="修改状态",response = BaseResult.class)
	public BaseResult updateState(@RequestBody PageConfigUpdateStateParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		PageConfigInfo entity= pageConfigMgrService.updateState(params,userInfo);
		if(entity!=null){
			pageCacheService.removePageConfigCache(entity.getPageNo());
		}
		return BaseResult.ok();
	}

	@GetMapping("/delete")
	@ApiOperation(value="删除",response = BaseResult.class)
	public BaseResult delete(@RequestParam("pageNo")String pageNo) {
		if(CheckUtils.isEmpty(pageNo)) {
			throw new BusinessException(BaseReturnEnum.PARAM_NULL);
		}
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		PageConfigInfo entity= pageConfigMgrService.delete(pageNo,userInfo);
		pageCacheService.removePageConfigCache(entity.getPageNo());
		return BaseResult.ok();
	}
	
}

